package com.example.piece.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.base.ext.getViewModel
import com.example.base.ext.tab_expand.indicator.BaseIndicator
import com.example.base.ext.tab_expand.indicator.LinearIndicator
import com.example.base.ext.tab_expand.tab.buildIndicator
import com.example.base.ext.tab_expand.tab.buildText
import com.example.base.ext.tab_expand.tab.toPx
import com.example.base.ext.tab_expand.text.BaseText
import com.example.base.ui.fragment.BaseFragment
import com.example.piece.R
import com.example.piece.databinding.FragmentGuanZhuBinding
import com.example.piece.databinding.FragmentZuoPinBinding
import com.example.piece.ui.adapter.ViewPagerAdapter
import com.example.piece.vm.UserViewModel


/**
 * 作品 喜欢
 */
class ZuoPinFragment : BaseFragment<FragmentZuoPinBinding>() {
    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }

    override fun getContentRes(): Int = R.layout.fragment_zuo_pin

    var type: Int = 0
    lateinit var targetUserId: String

    /**
     * @param type 0 作品   1喜欢
     */
    companion object {
        fun newInstance(targetUserId: String, type: Int): Fragment {
            val fragment = ZuoPinFragment()
            val bundle = Bundle()
            bundle.putString("targetUserId", targetUserId)
            bundle.putInt("type", type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        type = arguments?.getInt("type", 0)!!
        targetUserId = arguments?.getString("targetUserId", "").toString()
        mBinding.tab.buildIndicator<LinearIndicator>()
            .setAngle(4.toPx())
            .setHeight(BaseIndicator.MATCH)
            .setWidth(BaseIndicator.MATCH)
            .bind()
        mBinding.tab.buildText<BaseText>()
            .setNormalTextBold(true)
            .setSelectTextBold(true)
            .setNormalTextSize(14f)
            .setSelectTextSize(17f)
            .bind()
        val fragments = mutableListOf<Fragment>()
        val titles = listOf("文字.图片", "视频")
        when (type) {
            0 -> {
                fragments.add(ShouCangFragment.newInstance(targetUserId, 1))
                fragments.add(ZPVideoFragment.newInstance(targetUserId, 2))
            }
            1 -> {
                fragments.add(ShouCangFragment.newInstance(targetUserId, 3))
                fragments.add(ZPVideoFragment.newInstance(targetUserId, 4))
            }
        }


        mBinding.vp.adapter = ViewPagerAdapter(childFragmentManager, fragments, titles)
        mBinding.tab.setupWithViewPager(mBinding.vp)
        mBinding.vp.offscreenPageLimit = 5

    }

}